package api

import (
	"api_client/bassic/config"
	__ "api_client/bassic/proto"
	"api_client/handler/request"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func ReleaseVideo(c *gin.Context) {
	var req request.ReleaseVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数错误",
			"data": err.Error(),
		})
		return
	}
	r, err := config.VideoClient.ReleaseVideo(c, &__.ReleaseVideoReq{
		Id: req.Id,
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": r.Code,
		"msg":  r.Code,
		"data": r,
	})
}

func UploadVideo(c *gin.Context) {
	// 单文件
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	dst := "D:\\gowork\\src\\zg4\\2212a\\ks\\mango_tv\\upload\\" + file.Filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst+file.Filename)
	// 上传文件至指定的完整文件路径
	r, err := config.VideoClient.UploadVideo(c, &__.UploadVideoReq{
		VideoUrl: dst + file.Filename,
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": r.Code,
		"msg":  r.Code,
		"data": r.VideoUrl,
	})
}

func ManageVideo(c *gin.Context) {
	var req request.ManageVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "数据错误",
			"data": err.Error(),
		})
		return
	}
	r, err := config.VideoClient.ManageVideo(c, &__.ManageVideoReq{
		Title:       req.Title,
		Description: req.Description,
		VideoUrl:    req.VideoUrl,
		CoverUrl:    req.CoverUrl,
		Status:      req.Status,
		UserId:      req.UserId,
		CategoryId:  req.CategoryId,
		MemberId:    req.MemberId,
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": r.Code,
		"msg":  r.Code,
		"data": req,
	})
}
